package com.lanou.musicproject.adapter;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lanou.musicproject.R;
import com.lanou.musicproject.SQLite.MyManagerDatabase;
import com.lanou.musicproject.bean.DownloadedSong;
import com.squareup.picasso.Picasso;
import com.umeng.socialize.utils.Log;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by ztq on 15/10/30.
 * <p/>
 * 行布局里取出数据库的值
 */
public class DownManagerLvAdapter extends BaseAdapter {

    private List<DownloadedSong> data;
    private SQLiteDatabase db;
    private Context mContext;
    private String song, singer, image;
    private ArrayList<DownloadedSong>datas;

    private static final int PROCESSING = 1;
    private static final int FAILURE = -1;


    public DownManagerLvAdapter(Context mContext) {
        this.mContext = mContext;

        //初始化数据库
        MyManagerDatabase managerDatabase =
                new MyManagerDatabase(mContext, "downloaded.db", null, 1);
        db = managerDatabase.getWritableDatabase();
    }


    @Override
    public int getCount() {

        Log.i("--queue--", "sql1");

        //已执行
        queue(null, null);

        int d = (data != null && data.size() > 0 ? data.size() : 0);

        Log.i("adapter", d + "");

        return d;
    }

    @Override
    public Object getItem(int position) {

        return data != null && data.size() > 0 ? data.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;


        if (convertView == null) {
            //实例化
            Log.i("--queue--", "sql2");
            convertView = LayoutInflater.from(mContext).inflate(R.layout.download_item, null);
            holder = new ViewHolder();
            holder.songName = (TextView) convertView.findViewById(R.id.songName);
            holder.singerName = (TextView) convertView.findViewById(R.id.singerName);
            holder.percent = (TextView) convertView.findViewById(R.id.percentTv);
            holder.proBar = (ProgressBar) convertView.findViewById(R.id.proBar);
            holder.imageView = (ImageView) convertView.findViewById(R.id.imageIv);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        /*
        *
        * 数据库的查询
        * **/
        Log.i("--queue--", "sql3");
         data = queue(null, null);

        DownloadedSong dsong = (DownloadedSong) getItem(position);

        if (dsong != null) {

            for(DownloadedSong d : data){
                holder.songName.setText(data.get(position).getSong());
                holder.singerName.setText(data.get(position).getSinger());
                Picasso.with(mContext).load(data.get(position).getImage()).into(holder.imageView);

                Log.i("ddddd","song " + d.getSong() + " + singer " + d.getSinger());

            }
            Log.i("--queue--", "sql4");


        }





        return convertView;
    }

    //查询方法
    public List<DownloadedSong> queue(String selection, String[] selectionArgs) {

        Log.i("--queue--", "sql5");

        data = new ArrayList<>();
        Cursor cursor = db.query(MyManagerDatabase.downloaded, null,
                selection, selectionArgs, null, null, null);

        while (cursor.moveToNext()) {

            Log.i("--queue--", "cursor");

            int id_index = cursor.getColumnIndex("_id");
            int song_index = cursor.getColumnIndex(MyManagerDatabase.column_song);
            int singer_index = cursor.getColumnIndex(MyManagerDatabase.column_singer);
            int image_index = cursor.getColumnIndex(MyManagerDatabase.column_image);

            song = cursor.getString(song_index);
            singer = cursor.getString(singer_index);
            image = cursor.getString(image_index);

            Log.i("--queue--", "song  " + song);
            Log.i("--queue--", "singer  " + singer);
            Log.i("--queue--", "image " + image);

            DownloadedSong downloadedSong = new DownloadedSong(image, singer, song);
            data.add(downloadedSong);
        }


        return datas;
    }


    private class ViewHolder {
        public ProgressBar proBar;
        public TextView songName, singerName, percent;
        public ImageView imageView;
    }


}

